package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Product;
import com.service.ProductService;
import com.vo.QueryVo;
import com.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/** 产品
 * @author Sans
 */
@Controller
@RequestMapping("product")
@ResponseBody
public class ProductController {

    @Resource
    private ProductService productService;
    
    //商品列表
    @RequestMapping(value = "list" , method = RequestMethod.GET)
    public ResultVo<Product> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo){
        System.out.println("vo : " + vo);
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0){
            pageSize = 5;
        }
        PageInfo<Product> pageInfo = productService.queryByPage(pageNum , pageSize , vo);
        System.out.println("pageInfo : " + pageInfo);
        return new ResultVo<>(pageInfo);
    }
    
    //添加商品
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVo<Product> addProduct(Product product){
        if (product.getP_id() == null || "".equals(product.getP_id())){
            return new ResultVo<>(500,"请输入编号");
        }
        if (product.getP_name() == null || "".equals(product.getP_name())){
            return new ResultVo<>(500,"请输入名称");
        }
        if (product.getP_code() == null || "".equals(product.getP_code())){
            return new ResultVo<>(500,"请输入条形码");
        }
        if (product.getP_rule() == null || "".equals(product.getP_rule())){
            return new ResultVo<>(500,"请输入规格");
        }
        int i = productService.addProduct(product);
        if (i == 0) {
            return new ResultVo<>(500,"注意添加的参数");
        } else {
            return new ResultVo<>();
        }
        
    }
    
    //删除商品
    @RequestMapping(value = "{p_num}",method = RequestMethod.DELETE)
    public ResultVo<Product> delectProduct(@PathVariable("p_num") int id) {
        int i = productService.delectProduct(id);
        if (i == 0) {
            return new ResultVo<>(500,"异常");
        }
        return new ResultVo<>();
    }
    
    //更改商品
    @RequestMapping(value = "{p_num}" , method = RequestMethod.PUT)
    public ResultVo<Product> updateProduct(
            @PathVariable("p_num") int p_num, Product product){
        product.setP_num(p_num);
        int i = productService.updateProduct(product);
        if (i == 0){
            return new ResultVo<>(500,"请查看修改的参数");
        }
        return new ResultVo<>();
    }
    
    //查询key
    @RequestMapping(value = "{p_num}" , method = RequestMethod.GET)
    public ResultVo<Product> queryByKey(@PathVariable("p_num") int id){
        return new ResultVo<>(productService.queryByKey(id));
    }
    
}
